package com.tencent.qcloud.timchat.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.tencent.imsdk.TIMConversation;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.imsdk.ext.message.TIMConversationExt;
import com.wbao.dianniu.R;
import com.wbao.dianniu.logical.DnApplication;
import com.wbao.dianniu.logical.GlobalContext;


/**
 * 好友或群聊的会话
 */
public class NomalConversation extends Conversation {
    private String TAG = NomalConversation.class.getSimpleName();
    private TIMConversation conversation;
    //最后一条消息
    private Message lastMessage;


    public NomalConversation(TIMConversation conversation) {
        this.conversation = conversation;
        type = conversation.getType();
        identify = conversation.getPeer();
    }


    public void setLastMessage(Message lastMessage) {
        this.lastMessage = lastMessage;
    }


    /*@Override
    public int getAvatar() {
        switch (type) {
            case C2C:
                FriendProfile profile = FriendshipInfo.getInstance().getProfile(identify);
                int headRes = R.drawable.default_head_icon;
                if (null != profile) {
                    headRes = profile.getAvatarRes();
                }
                return headRes;
            case Group:
                return R.drawable.default_head_icon;
        }
        return 0;
    }*/

    /*@Override
    public String getAvatarUrl() {
        String headRes = "";
        switch (type) {
            case C2C:
                FriendProfile profile = FriendshipInfo.getInstance().getProfile(identify);
                if (null != profile) {
                    headRes = profile.getAvatarUrl();
                }
                return headRes;
            case Group:
                return headRes;
        }
        return headRes;
    }*/


    /**
     * 跳转到聊天界面或会话详情
     *
     * @param context 跳转上下文
     */
    @Override
    public void navToDetail(Context context) {
        ChatActivity.navToChat(context, identify, type, url, name, GlobalContext.cdndownUrl + GlobalContext.getHeadPic());
    }

    /**
     * 获取最后一条消息摘要
     */
    @Override
    public String getLastMessageSummary() {
        TIMConversationExt ext = new TIMConversationExt(conversation);
        if (ext.hasDraft()) {
            TextMessage textMessage = new TextMessage(ext.getDraft());
            if (lastMessage == null || lastMessage.getMessage().timestamp() < ext.getDraft().getTimestamp()) {
                return DnApplication.getContext().getString(R.string.conversation_draft) + textMessage.getSummary();
            } else {
                return lastMessage.getSummary();
            }
        } else {
            if (lastMessage == null) return "";
            return lastMessage.getSummary();
        }
    }

    /**
     * 获取名称
     */
    @Override
    public String getName() {
        if (TextUtils.isEmpty(name)) {
            name = identify;
        }
        /*if (type == TIMConversationType.Group) {
            if (TextUtils.isEmpty(name)) name = identify;
        } else {
            FriendProfile profile = FriendshipInfo.getInstance().getProfile(identify);
            if (null == profile) {
                if (TextUtils.isEmpty(name)) {
                    name = identify;
                }
            } else {
                name = profile.getName();
            }
        }*/
        return name;
    }

//    @Override
//    public void setName(String name) {
//        this.name = name;
//    }
//
//    @Override
//    public void setUrl(String url) {
//        this.url = url;
//    }
//
//    @Override
//    public String getUrl() {
//        return super.getUrl();
//    }

    /**
     * 获取未读消息数量
     */
    @Override
    public long getUnreadNum() {
        if (conversation == null) return 0;
        TIMConversationExt ext = new TIMConversationExt(conversation);
        return ext.getUnreadMessageNum();
    }

    /**
     * 将所有消息标记为已读
     */
    @Override
    public void readAllMessage() {
        if (conversation != null) {
            TIMConversationExt ext = new TIMConversationExt(conversation);
            ext.setReadMessage(null, null);
        }
    }


    /**
     * 获取最后一条消息的时间
     */
    @Override
    public long getLastMessageTime() {
        TIMConversationExt ext = new TIMConversationExt(conversation);
        if (ext.hasDraft()) {
            if (lastMessage == null || lastMessage.getMessage().timestamp() < ext.getDraft().getTimestamp()) {
                return ext.getDraft().getTimestamp();
            } else {
                return lastMessage.getMessage().timestamp();
            }
        }
        if (lastMessage == null) return 0;
        return lastMessage.getMessage().timestamp();
    }

    /**
     * 获取会话类型
     */
    public TIMConversationType getType() {
        return conversation.getType();
    }
}
